from .app import dlApp, pool
from core.models.types import SearchResult, TaskException
from playwright.sync_api import Page, Route
from typing import Sequence
import re
@dlApp.task(bind=True)
def search_zhihu(self, query: str, topk: int = 5):
    return ''

@dlApp.task(bind=True)
def search_baidu(self, query: str, topk: int = 5) -> Sequence[SearchResult]:
    if not pool:
        raise TaskException("Pool is not initialized")
    
    page: Page = pool.acquire_page()
    def handleRoute(route:Route, request):
        route.abort()

    page.route(re.compile(r"\.(jpg|png|svg)$"), handleRoute)
    url = f"https://www.baidu.com/s?wd={query}"
    page.goto(url)
    page.wait_for_load_state('networkidle')
    return []
